//
//  ZRWecomeView.m
//  SinaByOC
//
//  Created by 张锐 on 16/3/18.
//  Copyright © 2016年 zhangr. All rights reserved.
//

#import "ZRWecomeView.h"

#import <Masonry.h>

#import <UIImageView+WebCache.h>

@interface ZRWecomeView ()

@property (nonatomic,strong) UIImageView * iconImageView;

@property (nonatomic,strong) UILabel * welcomeLabel;

@property (nonatomic,strong) UIImageView * backgroundImageView;



@end

@implementation ZRWecomeView

/**
 对外接口
 */

-(void)setIconImage:(NSString *)imageURLString andWelcomeText:(NSString *)welcomeText{

    NSURL * url =[NSURL URLWithString:imageURLString];
    
    [self.iconImageView sd_setImageWithURL:url];

    self.welcomeLabel.text = [NSString stringWithFormat:@"%@欢迎回来噜",welcomeText];

}

/**
 *  重写构造方法
 */
- (instancetype)init
{
    self = [super init];
    if (self) {
        
        [self setupUI];
    
    }
    return self;
}

-(void)setupUI{
    
    [self addSubview:self.backgroundImageView];
    
    [self addSubview:self.iconImageView];
    
    [self addSubview:self.welcomeLabel];
    
    //设置约束
    
    [self.backgroundImageView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.left.right.bottom.equalTo(self);
        
    }];
    
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.mas_top).offset(370);
        
        make.centerX.equalTo(self.mas_centerX);
        
        make.width.equalTo(@80);
        
        make.height.equalTo(@80);
        
    }];
    
    [self.welcomeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.iconImageView.mas_bottom).offset(30);
        
        make.centerX.equalTo(self.mas_centerX);
        
        make.width.equalTo(@216);
        
    }];


}
//懒加载控件

-(UIImageView *)iconImageView{

    if (_iconImageView == nil) {
        
        _iconImageView = [[UIImageView alloc]init];
        
        _iconImageView.image = [UIImage imageNamed:@"avatar_default_big"];
        
        
        _iconImageView.layer.cornerRadius = 40;
        
        _iconImageView.layer.masksToBounds = YES;
        
        _iconImageView.layer.borderWidth = 2;
        
        _iconImageView.layer.borderColor = [UIColor darkGrayColor].CGColor;
        
    }
    
    return _iconImageView;


}
-(UILabel *)welcomeLabel{

    if (_welcomeLabel == nil) {
        
        _welcomeLabel =[[UILabel alloc]init];
        
        _welcomeLabel.text = @"欢迎回来";
        
        _welcomeLabel.textAlignment = NSTextAlignmentCenter;
        
        _welcomeLabel.textColor = [UIColor darkGrayColor];
        
        _welcomeLabel.font = [UIFont systemFontOfSize:14];
        
        _welcomeLabel.numberOfLines = 0;
        
    }
    return  _welcomeLabel;
}

-(UIImageView *)backgroundImageView{

    if (_backgroundImageView == nil) {
        
        _backgroundImageView = [[UIImageView alloc]init];
        
        _backgroundImageView.image = [UIImage imageNamed:@"ad_background"];
        
    }

    return _backgroundImageView;

}

-(void)startAnimationWithFinishedBlock:(void(^)())finishedBlock{
    
    [self.iconImageView mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.mas_top).offset(110);
        
    }];
    
    [UIView animateWithDuration:2 delay:0 usingSpringWithDamping:0.6 initialSpringVelocity:7 options:0 animations:^{
        
        [self layoutIfNeeded];
        
    } completion:^(BOOL finished) {
        
        finishedBlock();
        
    }];

}

@end
